package org.feifei.pet.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.feifei.basic.exception.BusinessException;
import org.feifei.basic.util.CommonResult;
import org.feifei.basic.util.PageList;
import org.feifei.pet.domain.Pet;
import org.feifei.pet.domain.SearchMasterMsg;
import org.feifei.pet.query.SearchMasterMsgQuery;
import org.feifei.pet.service.SearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "寻主接口",description = "寻主接口详细描述")
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private SearchMasterMsgService searchMasterMsgService;

    //获取所有对象：get
    //Get http://localhost:8080/searchMasterMsg
    @ApiOperation(value = "查询所有",notes = "不需参数")
    @GetMapping
    public List<SearchMasterMsg> getAll(){
            return searchMasterMsgService.getAll();
    }

    /**
     * 发布宠物寻主信息
     * @param msg
     * @param request
     * @return
     */
    @ApiOperation(value = "提交宠物申请")
    @PostMapping("/publish")
    public CommonResult AddPetSearchMasterMsg(@RequestBody  SearchMasterMsg msg, HttpServletRequest request) {
        try {
            searchMasterMsgService.pushlish(msg, request);
            return CommonResult.ok();
        } catch (BusinessException e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure("提交失败 ：" + e.getMessage());
        }
    }


    /**
     * 后台查看所有待处理的消息
     * @param query 查询对象
     * @param request 请求
     * @return
     */
    @ApiOperation("待处理")
    @PostMapping("/pending")
    public PageList<List<SearchMasterMsg>> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.pending(query, request);

    }

    /**
     * 后台查看所有已经处理的消息
     * @param query 查询对象
     * @param request 请求
     * @return
     */
    @ApiModelProperty("已处理")
    @PostMapping("/processed")
    public PageList<List<SearchMasterMsg>> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.processed(query, request);
    }

    @ApiModelProperty("已处理")
    @PostMapping("/userProccessed")
    public PageList<List<SearchMasterMsg>> userCheckMsg(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.user(query, request);
    }

    /**
     * 处理寻主消息
     * @param pet
     * @return
     */
    @PutMapping("/handle")
    public CommonResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            searchMasterMsgService.handle(pet, request);
            return CommonResult.ok();
        } catch (BusinessException e) {
            e.printStackTrace();
            return CommonResult.failure("处理失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure("系统错误，" + e.getMessage());
        }
    }
}
